{% extends "admin_base.html" %}

{% block title %}Add a New Server{% endblock %}

{% block content %}
<div class="col-9">
	<h3 class="nopad">Create New Server</h3><hr />
	{% if get.disp is defined and get.disp != 1 %}
		<div class="alert alert-danger">
			{% if get.disp == 'missing_args' %} Not all arguments were passed by the script.
			{% elseif get.disp == 'gsd_offline' %} The GSD daemon running on the server is not on or is blocked by your firewall. Please make sure it is accessible before creating a server.
			{% elseif get.disp == 's_fail' %} The server name you entered does not meet the requirements. Must be at least 4 characters, and no more than 35. Server name can only contain a-zA-Z0-9_-
			{% elseif get.disp == 'n_fail' %} The node selected does not seem to exist.
			{% elseif get.disp == 'ip_fail' %} The selected IP does not exist.
			{% elseif get.disp == 'port_fail' %} The selected port does not exist.
			{% elseif get.disp == 'port_full' %} The selected port is already in use.
			{% elseif get.disp == 'e_fail' %} The email you entered is invalid.
			{% elseif get.disp == 'p_fail' %} The passwords you entered did not match or were not at least 8 characters.
			{% elseif get.disp == 'a_fail' %} Account with that email does not exist in the system.
			{% elseif get.disp == 'm_fail' %} You entered a non-number for Disk and/or Memory.
			{% elseif get.disp == 'b_fail' %} You entered a non-number for Backup Files and/or Disk Space.
			{% endif %}
		</div>
	{% endif %}
	<form action="ajax/new/create.php" method="POST">
		<fieldset>
			<div class="well">
				<div class="row">
					<div class="form-group col-6 nopad">
						<label for="server_name" class="control-label">Server Name</label>
						<div>
							<input type="text" autocomplete="off" name="server_name" class="form-control" />
							<p class="text-muted" style="margin: 0 0 -10.5px;"><small><em>Character Limits: a-zA-Z0-9_- (Max 35 characters)</em></small></p>
						</div>
					</div>
					<div class="form-group col-6 nopad-right">
						<label for="email" class="control-label">Owner Email</label>
						<div>
							<input type="text" autocomplete="off" name="email" value="{% if get.email is defined %}{{ get.email }}{% endif %}" class="form-control" />
						</div>
					</div>
				</div>
			</div>
			<div class="well">
				<div class="row">
					<div class="form-group col-6 nopad">
						<label for="server_name" class="control-label">Server Node</label>
						<div>
							<select name="node" id="getNode" class="form-control">
								{% for node in nodes %}
									<option value="{{ node.id }}">{{ node.node }}</option>
								{% endfor %}
							</select>
						</div>
					</div>
				</div>
				<div class="alert alert-warning" id="noPorts" style="display:none;margin-bottom:10px;"><strong>Error:</strong> This node does not have any free ports available. Please select another node.</div>
				<div class="row">
					<span id="updateList">
						<div class="form-group col-6 nopad">
							<label for="server_ip" class="control-label">Server IP</label>
							<div>
								<select name="server_ip" class="form-control">
									<option value="---">Select a Node</option>
								</select>
							</div>
						</div>
						<div class="form-group col-6 nopad-right">
							<label for="server_port" class="control-label">Server Port</label>
							<div>
								<select name="server_port" class="form-control">
									<option value="---">Select a Node</option>
								</select>
							</div>
						</div>
					</span>
				</div>
			</div>
			<div class="well">
				<div class="row">
					<div class="form-group col-4 nopad">
						<label for="alloc_mem" class="control-label">Allocate Memory (in MB)</label>
						<div class="input-group">
							<input type="text" autocomplete="off" name="alloc_mem" class="form-control" />
							<span class="input-group-addon">MB</span>
						</div>
					</div>
					<div class="form-group col-4">
						<label for="alloc_disk" class="control-label">Allocate Disk Space (in MB)</label>
						<div class="input-group">
							<input type="text" autocomplete="off" name="alloc_disk" class="form-control" />
							<span class="input-group-addon">MB</span>
						</div>
					</div>
					<div class="form-group col-4 nopad-right">
						<label for="cpu_limit" class="control-label">CPU Limit</label>
						<div class="input-group">
							<input type="text" autocomplete="off" name="cpu_limit" class="form-control" />
							<span class="input-group-addon">%</span>
						</div>
					</div>
				</div>
				<div class="row"><p><small>If you do not want to limit CPU usage set the value to 0. To determine a value, take the number <em>physical</em> cores and multiply it by 100. For example, on a quad core system <code>(4 * 100 = 400)</code> there is 400% available. To limit a server to using half of a single core, you would set the value to <code>50</code>. To allow a server to use up to two physical cores, set the value to <code>200</code>.</small></p></div>
			</div>
			<div class="well">
				<div class="row">
					<div class="alert alert-success" style="display: none;margin-bottom:10px;" id="gen_pass"></div>
					<div class="form-group col-6 nopad">
						<label for="ftp_pass" class="control-label">FTP Password</label>
						<div class="input-group">
							<input type="password" autocomplete="off" name="ftp_pass" class="form-control" />
							<span class="input-group-btn">
								<button class="btn btn-success" id="gen_pass_bttn" type="button">Generate</button>
							</span>
						</div>
					</div>
					<div class="form-group col-6 nopad-right">
						<label for="ftp_pass_2" class="control-label">FTP Password (Again)</label>
						<div>
							<input type="password" autocomplete="off" name="ftp_pass_2" class="form-control" />
						</div>
					</div>
				</div>
			</div>
			<div class="well">
				<input type="submit" value="Create Server" class="btn btn-primary btn-sm" />
			</div>
		</fieldset>
	</form>
</div>
{% endblock %}

{% block javascript %}
<script type="text/javascript">
	function updatePortList(){
		$("#server_ip").change(function() {
			var ip = $(this).val().replace(/\./g, "\\.");
			$("[id^=node_]").hide();
			$("#node_"+ip).show();
		});
	}
	function updateList(){
		var activeNode = $('#getNode').val();
		$("#noPorts").hide();
		$.ajax({
			type: "POST",
			url: "ajax/new/load_list.php",
			data: {'node' : activeNode},
			success: function(data) {
				$('#updateList').html(data);
				updatePortList();
				return false;
			}
		});
		return false;
	}
	$(document).ready(function(){
		updateList();
		$("#gen_pass_bttn").click(function(){
			$.ajax({
				type: "GET",
				url: "add.php?do=generate_password",
				success: function(data) {
					$("#gen_pass").html('<strong>Generated Password:</strong> '+data);
					$("#gen_pass").slideDown();
					$('input[name="ftp_pass"]').val(data);
					$('input[name="ftp_pass_2"]').val(data);
					return false;
				}
			});
			return false;
		});
		$('#getNode').change(function(){
			updateList();
		});
		if($.urlParam('error') != null){

			var field = $.urlParam('error');
			var exploded = field.split('|');

				$.each(exploded, function(key, value) {

					$('[name="' + value + '"]').parent().parent().addClass('has-error');

				});

			var obj = $.parseJSON($.cookie('__TMP_pp_admin_newserver'));

				$.each(obj, function(key, value) {

					$('[name="' + key + '"]').val(value);

				});

		}
	});
</script>
{% endblock %}